package com.xiao.ztree.sys.controller;


import com.xiao.ztree.core.R;
import com.xiao.ztree.sys.entity.Article;
import com.xiao.ztree.sys.query.ArticleQuery;
import com.xiao.ztree.sys.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiaoCoder
 * @since 2021-04-29
 */
@Api(tags = "文章表 前端控制器", description = "用户表 前端控制器")
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/article")
public class ArticleController {

    private final IArticleService iArticleService;

    /**
     * 根据id获取的数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取文章数据",
            notes = "根据id获取文章数据")
    @GetMapping("/getOne/{id}")
    public R getOneById(@PathVariable Integer id) {
        return R.ok(iArticleService.getById(id));
    }

    /**
     * 获取所有文章信息
     *
     * @return
     */
    @ApiOperation(value = "获取所有文章信息",
            notes = "获取所有文章信息")
    @GetMapping("/list")
    public R list() {
        return R.ok(iArticleService.list());
    }

    /**
     * 保存文章
     *
     * @param article
     * @return
     */
    @ApiOperation(value = "保存文章",
            notes = "保存文章")
    @PostMapping("/save")
    public R save(Article article) {
        return R.ok(iArticleService.save(article));
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除文章",
            notes = "删除文章")
    @PostMapping("/del/{id}")
    public R del(@PathVariable("id") Integer id) {
        return R.ok(iArticleService.removeById(id));
    }

    /**
     * 条件搜索文章
     *
     * @param articleQuery
     * @return
     */
    @ApiOperation(value = "条件搜索文章",
            notes = "articleQuery")
    @GetMapping("/search")
    public R search(ArticleQuery articleQuery) {
        return R.ok(iArticleService.listAll(articleQuery));
    }
}
